package com.demo.code;

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;

public class SvnCheckout {
    public static void main(String[] args) throws SVNException {
        String svnUsername = "lijicai";
        String svnPassword = "lijicai123";
//        String svnUrl = "svn://10.252.176.49/svnrepo";
        String svnUrl = "svn://10.252.176.49/svnku";
        //svn检出
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(svnUsername, svnPassword.toCharArray());
        // 初始化版本库
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
        //建立SVN创建库连接
        SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
        // 创建身份验证管理器
        repository.setAuthenticationManager(authManager);
        DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
        //创建SVN实例管理器
        SVNClientManager clientManager = SVNClientManager.newInstance(options, authManager);

        SVNUpdateClient updateClient = clientManager.getUpdateClient();
        updateClient.setIgnoreExternals(false);
        try {
            //return updateClient.doCheckout(url, destPath, revision, revision,depth, false);
            long result = updateClient.doCheckout(SVNURL.parseURIEncoded(svnUrl), new File("D:\\data\\logs\\svncheckout"), SVNRevision.create(1), SVNRevision.create(29), SVNDepth.INFINITY, false);
            System.out.println("检出结果result==" + result);
        } catch (SVNException e) {
            throw new RuntimeException("检出SVN仓库失败：" + e.getMessage());
        } finally {
            repository.closeSession();
        }
        //原文链接：https://blog.csdn.net/yxf771hotmail/article/details/88311508
    }
}
